import PubSub.*;

import java.util.*;
import java.util.regex.*;

public class Client {
	public static final int PUSH = 1;
	public static final int PULL = 2;

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		String host = "localhost";
		String port = "1050";
		String username = "";
		String password = "";
		String cmd = "";
		int logic = 1;
		String topicName = "";
		SubscriberImpl subscriber = null;
		
		if(args.length==2) {
			host = args[0];
			port = args[1];
		} else if(args.length>2 || args.length==1) {
			System.out.println(
				"ERROR: Invalid parameters. Correct call is:"+
				"\n\tjava Client host port\n"+
				"Default settings ("+host+"-"+port+") will be used"
			);
		}

		do {
			System.out.print("\nInsert username: ");
			username = in.nextLine();
			System.out.print("Insert password: ");
			password = in.nextLine();
			subscriber = new SubscriberImpl(username, password, host, port);
		} while(!subscriber.bindClient());
		
		System.out.println(
			"\nSelect message notification logic:\n"+
			"\t1) push\n"+
			"\t2) pull"
		);
		do {
			System.out.print("\n>> ");
			cmd = in.nextLine();
			if(cmd.equals("1") || cmd.equals("2")) {
				logic = Integer.valueOf(cmd);
				subscriber.startListening(logic);
			} else {
				System.out.println("Type either 1 (for push) or 2 (for pull)");
			}
		} while(!cmd.equals("1") && !cmd.equals("2"));
		
		
		System.out.println("\nInsert command (type \"help\" or \"h\" for help): ");
		while(!cmd.equals("quit") && !cmd.equals("q")) {
			System.out.print("\n>> ");
			cmd = in.nextLine();
			// invoke operations
			if(cmd.equals("list") || cmd.equals("l")) {
				System.out.println("Topic list:");
				subscriber.printTopicList();
				
			} else if(cmd.equals("topic") || cmd.equals("t")) {
				System.out.print("Insert topic name: ");
				subscriber.addTopic(in.nextLine());
				
			} else if(cmd.equals("rmtopic") || cmd.equals("r")) {
				System.out.print("Insert topic name: ");
				subscriber.removeTopic(in.nextLine());

			} else if(cmd.equals("sub") || cmd.equals("s")) {
				System.out.print("Insert topic name: ");
				topicName = in.nextLine();
				System.out.println("Subscribing to topic "+topicName+"...");
				subscriber.subscribeTo(topicName);

			} else if(cmd.equals("unsub") || cmd.equals("u")) {
				System.out.print("Insert topic name: ");
				topicName = in.nextLine();
				System.out.println("Unubscribing to topic "+topicName+"...");
				subscriber.unsubscribeFrom(topicName);

			} else if(cmd.equals("mysub") || cmd.equals("y")) {
				System.out.println("Your subscriptions:");
				subscriber.getSubscriptions();

			} else if(cmd.equals("msg") || cmd.equals("m")) {
				System.out.print("Insert topic name: ");
				topicName = in.nextLine();
				System.out.println("Insert message to add in topic "+topicName+":");
				subscriber.addMessage(topicName, in.nextLine());

			} else if(cmd.equals("pull") || cmd.equals("p")) {
				if(logic==PULL) {
					subscriber.pullReceivedMessage();
				} else {
					System.out.print("Your message notification logic is not pull\n");
				}

			} else if(cmd.equals("all") || cmd.equals("a")) {
				System.out.print("Insert topic name: ");
				topicName = in.nextLine();
				subscriber.getMessageInTopic(topicName);

			} else if(cmd.equals("unseen") || cmd.equals("n")) {
				System.out.print("Insert topic name: ");
				topicName = in.nextLine();
				subscriber.getUnseenMessageInTopic(topicName);

			} else if(cmd.equals("quit") || cmd.equals("q")) {
				subscriber.unbindClient();
				System.out.println("Bye bye...");

			} else if(cmd.equals("help") || cmd.equals("h")) {
				System.out.println(
					"\n\thelp\t[h]\t Show this help\n"+
					"\tlist\t[l]\t List of all topics\n"+
					"\ttopic\t[t]\t Create a new topic\n"+
					"\trmtopic\t[r]\t Delete a new topic\n"+
					"\tsub\t[s]\t Subscribe to a topic\n"+
					"\tunsub\t[u]\t Unsubscribe to a topic\n"+
					"\tmysub\t[y]\t Show your subscribed topic\n"+
					"\tmsg\t[m]\t Insert a message in a topic\n"+
					"\tpull\t[p]\t Pull received messages in this session\n"+
					"\tall\t[a]\t Get all messages of a topic\n"+
					"\tunseen\t[n]\t Get unseen messages of a topic\n"+
					"\tquit\t[q]\t Quit and exit program"
				);
			} else if(cmd.equals("")) {
				continue;
			} else {
				System.out.println("Unrecognized command...");
			}
		}
		
		System.exit(0);
	}

}